package com.apes.framework.plugin.eventbus;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 功能：EventBus 配置
 *
 * @author xul
 * @create 2017-11-18 10:04
 */
@Configuration
public class EventBusConfiguration {

    @Value("${spring.eventBus.async.nThreads:50}")
    private int nThreads;

    /**
     * 功能：创建同步事件总线
     */
    @Bean
    public EventBus getEventBus() {
        EventBus bus = new EventBus();
        return bus;
    }

    /**
     * 功能：创建异步事件总线
     */
    @Bean
    public AsyncEventBus getAsyncEventBus() {
        ExecutorService es = getExecutorService();
        AsyncEventBus bus = new AsyncEventBus(es);
        return bus;
    }

    @Bean(name = "asyncEventExecutor")
    public ExecutorService getExecutorService() {
        ExecutorService es = Executors.newFixedThreadPool(nThreads);
        return es;
    }

}

